﻿using DocumentFormat.OpenXml.Lite.Shared;
using DocumentFormat.OpenXml.Lite.Xml;
using DocumentFormat.OpenXml.Lite.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Schema;
using System.Xml.Serialization;
using DocumentFormat.OpenXml.Lite.Core;

namespace DocumentFormat.OpenXml.Lite.WordprocessingML.Word.Table.RowProperties
{
    public abstract class SimpleRowProperties
    {
        [XmlAttribute("id", Namespace = Constants.WordMain, Form = XmlSchemaForm.Qualified)]
        public int Id { get; set; }

        [XmlAttribute("author", Namespace = Constants.WordMain, Form = XmlSchemaForm.Qualified)]
        public string Author { get; set; }

        [XmlAttribute("date", Namespace = Constants.WordMain, Form = XmlSchemaForm.Qualified)]
        public DateTime Date { get; set; }
    }

    [XmlRoot("ins", Namespace = Constants.WordMain)]
    public class InsertedRow
    {
        public InsertedRow() { }
    }

    [XmlRoot("del", Namespace = Constants.WordMain)]
    public class DeletedRow
    {
        public DeletedRow() { }
    }

    [XmlRoot("trPrChange", Namespace = Constants.WordMain)]
    public class RowChange
    {
        [XmlElement("trPr", Namespace = Constants.WordMain)]
        public XmlList<ITableRowPropElement> Properties { get; set; }

        public RowChange()
        {
            Properties = new XmlList<ITableRowPropElement>();
            Properties.Changing += Properties_Changing;
        }

        void Properties_Changing(object sender, ListEventArgs<ITableRowPropElement> args)
        {
            if (args.Type == ListEventType.Remove)
                return;

            foreach (var obj in args.Objects)
            {
                if (obj.GetType().IsInheritedFrom(typeof(ITableRowPropElement)))
                {
                    var element = Properties.FirstOrDefault(e => e.GetType() == obj.GetType());
                    if (obj != null)
                    {
                        throw new OpenXmlException("Element with the same type (" +
                            element.GetType().ToString() + ") already added to list: " + Properties.ToString());
                    }
                }
            }
        }
    }
}
